package com.koron.sinoprobe.web.ledger.service.impl;

import com.alibaba.fastjson.JSON;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.common.CommonUtils;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProjectConclude;
import com.koron.sinoprobe.web.ledger.bean.query.SpProjectConcludeQuery;
import com.koron.sinoprobe.web.ledger.mapper.SpProjectConcludeMapper;
import com.koron.sinoprobe.web.ledger.service.SpProjectConcludeService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author twcao
 * @title: SpProjectConcludeServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 项目办结service实现
 * @date 2022/2/1114:11
 */
@Service
public class SpProjectConcludeServiceImpl implements SpProjectConcludeService {

    @Override
    @TaskAnnotation("queryById")
    public SpProjectConclude queryById(SessionFactory factory, String id) {
        SpProjectConcludeMapper mapper = factory.getMapper(SpProjectConcludeMapper.class);
        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<SpProjectConclude> queryList(SessionFactory factory, SpProjectConcludeQuery query) {
        SpProjectConcludeMapper mapper = factory.getMapper(SpProjectConcludeMapper.class);
        return mapper.queryList(query);
    }

    @Override
    @TaskAnnotation("saveOrUpdate")
    public SpProjectConclude saveOrUpdate(SessionFactory factory, SpProjectConclude bean) {
        Assert.notNull(bean, "项目办结不能为空");
        Assert.hasText(bean.getProjectId(), "项目id不能为空");
        CommonUtils.update(bean, ThreadLocalContext.get());
        bean.setId(StringUtils.isEmpty(bean.getId()) ? CodeTools.getCode32() : bean.getId());
        // 如果没有传入状态，则初始化为草稿
        bean.setStatus(StringUtils.isEmpty(bean.getStatus()) ? Constants.ConcludeStatus.DRAFT : bean.getStatus());
        SpProjectConcludeMapper mapper = factory.getMapper(SpProjectConcludeMapper.class);
        mapper.saveOrUpdate(bean);
        return bean;
    }

    @Override
    @TaskAnnotation("deleteById")
    public Boolean deleteById(SessionFactory factory, String id) {
        SpProjectConcludeMapper mapper = factory.getMapper(SpProjectConcludeMapper.class);
        return mapper.deleteById(id) > 0;
    }

    @Override
    @TaskAnnotation("workflowUpdate")
    public Boolean workflowUpdate(SessionFactory factory, RedirectQuery redirectQuery) {
        Integer operation = redirectQuery.getOperation();
        SpProjectConclude bean = JSON.parseObject(redirectQuery.getFormVariables(), SpProjectConclude.class);
        switch (operation) {
            case 256: // 撤回
            case 1: // 驳回
                // 驳回的节点编号
                bean.setStatus(Constants.ConcludeStatus.DRAFT);
                saveOrUpdate(factory, bean);
                break;
            case 32: // 同意
                bean.setStatus(Constants.ConcludeStatus.WAIT_REVIEW);
                saveOrUpdate(factory, bean);
                break;
        }
        return true;
    }

    @Override
    @TaskAnnotation("auditFinish")
    public SpProjectConclude auditFinish(SessionFactory factory, SpProjectConclude bean) {
        bean.setStatus(Constants.ConcludeStatus.FINISH);
        return saveOrUpdate(factory, bean);
    }
}
